﻿using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using dpa.Library.Models;
using dpa.Library.Services;

namespace dpa.Library.ViewModels;

public partial class DetailViewModel(IFavoriteStorage favoriteStorage) : ViewModelBase
{
    public override void SetParameter(object parameter)
    {
        if (parameter is not Models.Poetry poetry)
        {
            return;
        }

        Poetry = poetry;
    }

    [ObservableProperty] private Poetry _poetry;


    [ObservableProperty] private Favorite _favorite;

    [ObservableProperty] private bool _isLoading;


    [RelayCommand]
    private async Task OnLoadedAsync()
    {
        IsLoading = true;
        var favorite = await favoriteStorage.GetFavoriteAsync(Poetry.Id) ??
                       new Favorite
                       {
                           PoetryId = Poetry.Id
                       };
        Favorite = favorite;
        IsLoading = false;
    }

    [RelayCommand]
    private async Task FavoriteSwitchClickedAsync()
    {
        IsLoading = true;
        await favoriteStorage.SaveFavoriteAsync(Favorite);
        IsLoading = false;
    }
}